package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddLink;
import com.itheima.ydd.service.IYddLinkService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_link")
public class YddLinkController {
    @Autowired
    private IYddLinkService iYddLinkService;

    /**
     * 分页查询友情链接详情
     * @param type
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(int type, int page, int limit){
        //分页构建器
        Page<YddLink> linkPage = new Page<>(page,limit);
        //构建条件查询器
        LambdaQueryWrapper<YddLink> lqw = new LambdaQueryWrapper<>();
        //根据类型查询结果
        lqw.eq(YddLink::getType,type);
        //根据id降序
        lqw.orderByDesc(YddLink::getId);
        //调用分页方法
        iYddLinkService.page(linkPage,lqw);
        //调用方法获得集合
        List<YddLink> list = linkPage.getRecords();
        //创建Map集合
        Map<String,Object> Map = new HashMap<>();
        Map.put("count",list.size());
        Map.put("totalPages",linkPage.getPages());
        Map.put("pageSize",limit);
        Map.put("current",linkPage.getCurrent());
        Map.put("data",list);
        //返回结果
        return ResultData.ok("查询成功",Map);
    }

    /**
     * 添加友情链接
     * @param yddLink
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddLink yddLink){
        iYddLinkService.save(yddLink);
        return ResultData.ok("添加友情链接数据成功","");
    }

    /**
     * 根据id删除友情链接
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(int id){
        iYddLinkService.removeById(id);
        return ResultData.ok("删除友情链接成功","");
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(int id){
        YddLink yddLink = iYddLinkService.getById(id);
        return ResultData.ok("",yddLink);
    }

    /**
     * 修改数据
     * @param yddLink
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddLink yddLink){
        iYddLinkService.updateById(yddLink);
        return ResultData.ok("修改数据成功","");
    }

}
